package br.com.fiaproupas.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.fiaproupas.model.Cliente;
import br.com.fiaproupas.util.RetornaJson;
import br.com.fiaproupas.util.RetornaLista;

/**
 * Servlet implementation class InformaNomeServlet
 */
@WebServlet("/informaNomeServlet")
public class InformaNomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InformaNomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cpfCliente = request.getParameter("cpfCliente");
		String codCliente = "";
		boolean clienteEncontrado = false;
		
		RetornaJson retornaJson = new RetornaJson();
		String clienteS = retornaJson.retornaJsonClientes();
		
		RetornaLista retornaLista = new RetornaLista();
		List<Cliente> clientes = retornaLista.buscaClientes(clienteS);
		
		for(Cliente buscaCliente: clientes) {
			if(cpfCliente.trim().equalsIgnoreCase(buscaCliente.getCpf().trim())) {
				
				codCliente = buscaCliente.getIdCliente();
				clienteEncontrado = true;
				break;
			}
		}
		
		
		if(clienteEncontrado) {
			
			request.setAttribute("codCliente", codCliente);
			RequestDispatcher dispatcher = request.getRequestDispatcher("solicitapedido.jsp");  
			dispatcher.forward(request, response);
	
		}else {
			
			request.setAttribute("codCliente", codCliente);
			RequestDispatcher dispatcher = request.getRequestDispatcher("clientenaoencontrado.jsp");  
			dispatcher.forward(request, response);
			
		}
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String codCliente = request.getParameter("cpfCliente");
		boolean clienteEncontrado = false;
		
		RetornaJson retornaJson = new RetornaJson();
		String clienteS = retornaJson.retornaJsonClientes();
		
		RetornaLista retornaLista = new RetornaLista();
		List<Cliente> clientes = retornaLista.buscaClientes(clienteS);
		
		for(Cliente buscaCliente: clientes) {
			if(codCliente.trim().equalsIgnoreCase(buscaCliente.getCpf().trim())) {
				
				codCliente = buscaCliente.getIdCliente();
				clienteEncontrado = true;
				break;
			}
		}
		
		
		if(clienteEncontrado) {
			
			request.setAttribute("codCliente", codCliente);
			RequestDispatcher dispatcher = request.getRequestDispatcher("solicitapedido.jsp");  
			dispatcher.forward(request, response);
	
		}else {
			
			request.setAttribute("codCliente", codCliente);
			RequestDispatcher dispatcher = request.getRequestDispatcher("clientenaoencontrado.jsp");  
			dispatcher.forward(request, response);
			
		}
	}

}
